package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

/**
 *  程序启动后,要求用户输入标题和正文,然后将数据保存到article表中
 */
public class Test2 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection())
        {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入标题:");
            String title = scanner.nextLine();
            System.out.println("请输入正文:");
            String content = scanner.nextLine();
            String sql="insert into article(title,content) " +
                    " values(?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,title);
            ps.setString(2,content);

            int i = ps.executeUpdate();
            if(i>0)
            {
                System.out.println("保存成功");
            }
            else
            {
                System.out.println("保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
